<?php
/*
 * @Author: winner
 * @Date: 2022-04-23 22:44:26
 * @LastEditTime: 2022-04-27 13:09:19
 * @LastEditors: winner
 * @Description: 
 */
/*
 * @Author: winner
 * @Date: 2022-04-23 21:57:31
 * @LastEditTime: 2022-04-23 22:38:02
 * @LastEditors: winner
 * @Description: 
 */
// +----------------------------------------------------------------------
// | ShopXO 国内领先企业级B2C免费开源电商系统
// +----------------------------------------------------------------------
// | Copyright (c) 2011~2099 http://shopxo.net All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( https://opensource.org/licenses/mit-license.php )
// +----------------------------------------------------------------------
// | Author: Devil
// +----------------------------------------------------------------------
namespace app\admin\controller;

use app\service\UserService;
use app\service\ConfigService;
use app\service\MerchantService;
use base\Excel;

/**
 * SubMerchant设置
 * @desc 通莞金服子商户入网设置
 * @author   winner
 * @version  0.0.1
 * @datetime 2016-12-01T21:51:08+0800
 */
class Merchant extends Common
{
	/**
	 * 构造方法
	 * @author   Devil
	 * @blog     http://gong.gg/
	 * @version  0.0.1
	 * @datetime 2016-12-03T12:39:08+0800
	 */
	public function __construct()
	{
		// 调用父类前置方法
		parent::__construct();
		// 登录校验
		$this->IsLogin();
		// 权限校验
		$this->IsPower();
	}

	/**
     * [Index 子商户参数配置]
     * @author   Devil
     * @blog     http://gong.gg/
     * @version  0.0.1
     * @datetime 2016-12-06T21:31:53+0800
     */
	public function Index()
	{
		// 总数
		$total = MerchantService::MerchantTotal($this->form_where);

		// 分页
		$page_params = [
			'number'	=>	$this->page_size,
			'total'		=>	$total,
			'where'		=>	$this->data_request,
			'page'		=>	$this->page,
			'url'		=>	MyUrl('admin/merchant/index'),
		];
		$page = new \base\Page($page_params);
		// 获取数据列表
		$data_params = [
			'where'         => $this->form_where,
			'm'             => $page->GetPageStarNumber(),
			'n'             => $this->page_size,
			'order_by'      => $this->form_order_by['data'],
		];

		$ret = MerchantService::MerchantList($data_params);

		// 基础参数赋值
		MyViewAssign('params', $this->data_request);
		MyViewAssign('page_html', $page->GetPageHtml());
		MyViewAssign('data_list', $ret['data']);
		return MyView();
	}

	/**
	 * [ConfSaveInfo 子商户参数配置页面]
	 * @author   winner
	 * @version  0.0.1
	 */
	public function ConfigIndex()
	{
		MyViewAssign('data', ConfigService::ConfigList());
		return MyView('config');
	}

	/**
	 * [ConfSaveInfo 子商户参数保存]
	 * @author   winner
	 * @version  0.0.1
	 */
	public function SaveConf()
	{
		return ConfigService::ConfigSave($_POST);
	}

	/**
	 * 子商户入网编辑页面
	 */
	public function SaveInfo()
	{
			// 参数
		$params = $this->data_request;
		// 用户编辑
		$data = [];
		if(!empty($params['id']))
		{
			$data_params = [
				'where'		=> ['id'=>$params['id']],
				'm'			=> 0,
				'n'			=> 1,
			];
			$ret = MerchantService::MerchantList($data_params);
			if(empty($ret['data'][0]))
			{
				return $this->error('子商户信息不存在', MyUrl('admin/merchant/index'));
			}
		}
		// 商户类型
		MyViewAssign('mertype', MyConst('tgpay_mer_type'));
		// 获取地区
		$likes = isset($this->data_request['name']) ? 'name like %' . $this->data_request['name'] . '%' : '';
		$params = [
			'where' => $likes
		];
		$bankcodes = MerchantService::BankNode($params);

		// 数据
		unset($params['id']);
		MyViewAssign('data', $data);
		MyViewAssign('params', $params);
		MyViewAssign('bankcodes', $bankcodes);
		return MyView();
	}
	/**
	 * [Save 配置数据保存]
	 * @author   Devil
	 * @blog     http://gong.gg/
	 * @version  0.0.1
	 * @datetime 2017-01-02T23:08:19+0800
	 */
	public function Save()
	{
		// 是否ajax
		if (!IS_AJAX) {
			return $this->error('非法访问');
		}
		// 开始操作
		$params = $this->data_post;
		
		//上传图片
		if (isset($params['idcard_front']) && !empty($params['idcard_front'])) {
			$idcard_front = explode('/', $params['idcard_front']);
			$img_ret = MerchantService::upload(['imgname' => end($idcard_front), 'img_url' => $params['idcard_front']]);
			if($img_ret['returnCode']!= 'REG00000'){
				return DataReturn('身份证人脸面照片上传失败',-100);
			}
			$params['idcard_front'] = $img_ret['merQualUrl'];
		}
		if (isset($params['idcard_back']) && !empty($params['idcard_back'])) {
			$idcard_back = explode('/', $params['idcard_back']);
			$img_ret= MerchantService::upload(['imgname' => end($idcard_back), 'img_url' => $params['idcard_back']]);
			if ($img_ret['returnCode'] != 'REG00000') {
				return DataReturn('身份证国徽面照片上传失败', -100);
			}
			$params['idcard_back'] = $img_ret['merQualUrl'];
		}
		if (isset($params['hand_idcard']) && !empty($params['hand_idcard'])) {
			$hand_idcard = explode('/', $params['hand_idcard']);
			$img_ret = MerchantService::upload(['imgname' => end($hand_idcard), 'img_url' => $params['hand_idcard']]);
			if ($img_ret['returnCode'] != 'REG00000') {
				return DataReturn('手持身份证照片上传失败', -100);
			}
			$params['hand_idcard'] = $img_ret['merQualUrl'];
		}
		if (isset($params['settle_bank_card']) && !empty($params['settle_bank_card'])) {
			$settle_bankcard = explode('/', $params['settle_bank_card']);
			$img_ret = MerchantService::upload(['imgname' => end($settle_bankcard), 'img_url' => $params['settle_bank_card']]);
			if ($img_ret['returnCode'] != 'REG00000') {
				return DataReturn('结算银行卡照片上传失败', -100);
			}
			$params['settle_bankcard'] = $img_ret['merQualUrl'];
		}
		//获取商户所在地址编号，银行开户行编号
		$bank_ret = MerchantService::GetBankRegion([
			'province' => $params['province'],
			'meprovince' => $params['meprovince'],
			'city' => $params['city'],
			'mecity' => $params['mecity'],
			'county' => $params['county'],
			'mecounty' => $params['mecounty'],
		]);
		$params['bank_province'] = $bank_ret['province']['code'];
		$params['bank_city'] = $bank_ret['city']['code'];
		$params['bank_province'] = $bank_ret['province']['code'];
		$params['mer_province'] = $bank_ret['meprovince']['code'];
		$params['mer_city'] = $bank_ret['mecity']['code'];
		$params['mer_district'] = $bank_ret['mecounty']['code'];
		$params['mer_address'] = $bank_ret['mecounty']['name'];
		//提交入网信息
		$in_ret = MerchantService::in($params);
		if($in_ret['returnCode'] != 'REG00000'){
			return DataReturn($in_ret['returnMsg'],-100);
		}
		//清除多余信息
		array_diff_key($params,['province'=>0,'city'=>0, 'county'=>0, 'meprovince' => 0, 'mecity' => 0, 'mecounty' => 0]);
		//插入子商户数据库
		$params['requestno'] = $in_ret['requestNo'];
		$params['externalid'] = $in_ret['externalId'];
		$params['merchantno'] = $in_ret['merchantNo'];
		$params['add_time'] = $params['upd_time'] = time();

		return MerchantService::MerchantAdd($params);
	}

	//获取银行省市县编号
	public function Node()
	{
		// 是否ajax请求
		if (!IS_AJAX) {
			$this->error('非法访问');
		}
		// 获取地区
		$params = [
			'where' => [
				'pid'   => isset($this->data_request['pid']) ? intval($this->data_request['pid']) : 0,
			],
		];
		$data = MerchantService::RegionNode($params);
		return DataReturn('操作成功', 0, $data);
	}

	//获取银行代码
	public function BankCode()
	{
		// 是否ajax请求
		if (!IS_AJAX) {
			$this->error('非法访问');
		}
		// 获取地区
		$params = [
			'like' => [
				'name'   => isset($this->data_request['name']) ? intval($this->data_request['name']) : 0,
			],
		];
		$data = MerchantService::BankNode($params);
		return DataReturn('操作成功', 0, $data);
	}

	//通过接口获取支行信息数据
	public function BankNo()
	{
		// 是否ajax
		if (!IS_AJAX) {
			return $this->error('非法访问');
		}
		// 开始操作
		$params = $this->data_post;
		//获取省市编号
		$bank_ret = MerchantService::GetBankRegion(['province'=>$params['province'], 'city' => $params['city']]);
		//获取银行编号
		$code_ret = MerchantService::code([
			'head_bankno' => $params['code'],
			'bank_province' => $bank_ret['province']['code'],
			'bank_city' => $bank_ret['city']['code']
		]);
		if ($code_ret['returnCode'] != 'REG00000' || !is_array($code_ret['branchBankInfo'])) {
			return DataReturn('sucess', 0,['-1'=>'查无支行']);
		}
		$bank_code = $code_ret['branchBankInfo'];
		return DataReturn('sucess',0,$bank_code);
		///$params['bank_code'] = strval($bank_code);
	}

}
?>